package com.mapfinal.resource.postgis;

import com.jfinal.plugin.activerecord.DbPro;
import com.mapfinal.server.MapfinalServer;
import com.mapfinal.server.layer.ServerDataStore;
import com.mapfinal.server.layer.model.MfDatastore;
import com.mapfinal.server.layer.model.MfLayer;

public class PostgisServerDataStore implements ServerDataStore {

	private MfDatastore mfDatastore;
	private PostgisDataStore postgisDataStore;
	private int status = 0;

	public PostgisServerDataStore(MfDatastore mfDatastore) {
		this.mfDatastore = mfDatastore;
		postgisDataStore = new PostgisDataStore(mfDatastore.getName(), mfDatastore.getUrl(), mfDatastore.getUser(),
				mfDatastore.getPassword(), mfDatastore.getSchame());
	}

	public DbPro db() {
		return postgisDataStore != null ? postgisDataStore.db() : null;
	}
	
	@Override
	public boolean start() {
		// TODO Auto-generated method stub
		postgisDataStore.start();
		status = 1;
		return true;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		postgisDataStore.stop();
		status = 0;
		return true;
	}

	@Override
	public int status() {
		// TODO Auto-generated method stub
		return status;
	}

	@Override
	public PostgisServerLayer getServerLayer(String name) {
		// TODO Auto-generated method stub
		if(mfDatastore==null) return null;
		MfLayer mfLayer = MapfinalServer.me().getMfLayer(name);
		PostgisServerLayer serverLayer = new PostgisServerLayer(mfLayer); 
		return serverLayer;
	}

	@Override
	public Integer getDataStoreId() {
		// TODO Auto-generated method stub
		return mfDatastore == null ? null : mfDatastore.getId();
	}

	public MfDatastore getDatastore() {
		return mfDatastore;
	}

	public void setDatastore(MfDatastore mfDatastore) {
		this.mfDatastore = mfDatastore;
	}

	@Override
	public MfDatastore getMfDatastore() {
		// TODO Auto-generated method stub
		return mfDatastore;
	}

	@Override
	public PostgisDataStore getDataStore() {
		// TODO Auto-generated method stub
		return postgisDataStore;
	}

}
